package logic

import (
	"context"
	"hm-zero-mall/apps/user/models"
	"time"

	"hm-zero-mall/apps/user/rpc/internal/svc"
	"hm-zero-mall/apps/user/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserInfoLogic {
	return &UpdateUserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UpdateUserInfo 更新用户信息
func (l *UpdateUserInfoLogic) UpdateUserInfo(in *pb.UserInfo) (*pb.Empty, error) {
	user := new(models.User)
	user.Id = in.Id
	user.UpdateTime = time.Now().Unix()
	if in.Username != "" {
		user.Username = in.Username
	}
	if in.Password != "" {
		user.Password = in.Password
	}
	if in.Phone != "" {
		user.Phone = in.Phone
	}
	if in.Status != 0 {
		user.Status = in.Status
	}
	err := l.svcCtx.UserModel.Update(l.ctx, user)
	return &pb.Empty{}, err
}
